package com.iscas.tech.controller;

import com.iscas.tech.entity.AtNewsinfo;
import com.iscas.tech.param.PageParam;
import com.iscas.tech.service.AtNewsinfoService;
import com.iscas.tech.utils.ResponseUtil;
import com.iscas.tech.vo.FileWithNews;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.shiro.util.LoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 新闻信息表 控制器
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@RestController
@RequestMapping("/news")
@Api(value = "新闻信息表API", tags = {"新闻信息表"})
public class AtNewsinfoController extends BaseController {

    @Autowired
    private AtNewsinfoService atNewsinfoService;

    /**
     * 添加新闻信息表
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加新闻信息表", response = ApiResult.class)
    public ApiResult<Boolean> addNews(@RequestBody FileWithNews param) throws Exception {
        if (LoginUtil.getUserId() == null) {
            return ApiResult.fail(ApiCode.UNAUTHORIZED);
        }
        param.setCreateTitle(LoginUtil.getUserId().intValue());
        return ApiResult.result(atNewsinfoService.saveNews(param));
    }

    /**
     * 修改新闻信息表
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改新闻信息表", response = ApiResult.class)
    public ApiResult<Boolean> updateNewsInfo(@RequestBody FileWithNews param) throws Exception {
        if (LoginUtil.getUserId() == null) {
            return ApiResult.fail(ApiCode.UNAUTHORIZED);
        }
        param.setCreateTitle(LoginUtil.getUserId().intValue());
        return ApiResult.result(atNewsinfoService.updateNewsInfo(param));
    }

    /**
     * 删除新闻信息表
     */
    @RequestMapping(value = "delete", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "删除新闻信息表", response = ApiResult.class)
    public ApiResult<Boolean> deleteNewsInfo(String ids) throws Exception {
        return ApiResult.result(atNewsinfoService.deleteNewsInfo(ids));
    }

    /**
     * 获取新闻详情
     *
     * @param id       新闻主键ID
     * @param response 响应类
     * @throws Exception 查询异常
     */
    @RequestMapping(value = "info", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新闻信息表详情", response = FileWithNews.class)
    public void getNewsInfo(Integer id, HttpServletResponse response) throws Exception {
        ResponseUtil.write(response, ApiResult.ok(atNewsinfoService.get(id)));
    }

    /**
     * 分页获取新闻列表
     *
     * @param pageParam 分页参数
     * @param newsType  新闻类型
     * @throws Exception 查询异常
     */
    @GetMapping("/getPageList")
    @ApiOperation(value = "分页获取新闻列表", response = AtNewsinfo.class)
    public void getNewsPageList(HttpServletResponse response, PageParam pageParam, Integer newsType) throws Exception {
        ResponseUtil.write(response, ApiResult.ok(atNewsinfoService.getNewsPageList(pageParam, newsType)));
    }

    /**
     * 发布新闻
     *
     * @param id 新闻主键ID
     * @return 发布结果
     */
    @PostMapping("/release")
    @ApiOperation(value = "发布新闻")
    public ApiResult<Boolean> releaseNews(Integer id) {
        return ApiResult.result(atNewsinfoService.releaseNews(id));
    }
}

